﻿using System;
using System.IO;
using System.Windows.Forms;

namespace AdvancedScriptEditor
{
    public partial class FunctionEditor : Form
    {
        FunctionList functionList;
        string appPath;

        public FunctionEditor(FunctionList FunctionList)
        {
            InitializeComponent();
            functionList = FunctionList;
            appPath = Properties.Settings.Default.AppPath;
        }

        private void FunctionEditor_Load(object sender, EventArgs e)
        {
            listbx_Functions.Items.AddRange(functionList.Functions);
            listbx_Functions.SelectedIndex = 0;
        }

        private void listbx_Functions_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtbx_FunctionName.Text = listbx_Functions.SelectedItem.ToString();

            int index = -1;
            for (int i = 0; i < functionList.Functions.Length; i++)
            {
                string fbFunc = functionList.Functions[i];
                string lbFunc = txtbx_FunctionName.Text.ToString();
                if (fbFunc == lbFunc)
                    index = i;
            }
            if (index != -1)
                rtb_Description.Text = functionList.Descriptions[index];
            else
                rtb_Description.Text = "No function description found.";
        }

        private void txtbx_FunctionName_TextChanged(object sender, EventArgs e)
        {
            if (txtbx_FunctionName.Text != listbx_Functions.SelectedItem.ToString())
                btn_SaveName.Enabled = true;
            else
                btn_SaveName.Enabled = false;
        }

        private void rtb_Description_TextChanged(object sender, EventArgs e)
        {
            string text = "-1";
            int index = -1;
            for (int i = 0; i < functionList.Functions.Length; i++)
            {
                string fbFunc = functionList.Functions[i];
                string lbFunc = txtbx_FunctionName.Text.ToString();
                if (fbFunc == lbFunc)
                    index = i;
            }
            if (index != -1)
                text = functionList.Descriptions[index];
            else
                text = "No function description found.";

            if (rtb_Description.Text != text)
                btn_SaveDescription.Enabled = true;
            else
                btn_SaveDescription.Enabled = false;
        }

        private void btn_SaveName_Click(object sender, EventArgs e)
        {
            string prevText = rtb_Description.Text;
            int index = listbx_Functions.SelectedIndex;
            functionList.Functions[index] = txtbx_FunctionName.Text;
            listbx_Functions.Items.Clear();
            listbx_Functions.Items.AddRange(functionList.Functions);
            listbx_Functions.SelectedIndex = index;
            rtb_Description.Text = prevText;
            btn_SaveName.Enabled = false;
        }

        private void btn_SaveDescription_Click(object sender, EventArgs e)
        {
            int index = listbx_Functions.SelectedIndex;
            functionList.Descriptions[index] = rtb_Description.Text;
            listbx_Functions.SelectedIndex = index;
            btn_SaveDescription.Enabled = false;
        }

        private void btn_Accept_Click(object sender, EventArgs e)
        {
            functionList.Save(appPath + "\\functions.txt", appPath + "\\descriptions.txt");
            MessageBox.Show("Changes saved.");

            if (File.Exists(appPath + "\\tempfunctions.txt"))
                File.Delete(appPath + "\\tempfunctions.txt");
            if (File.Exists(appPath + "\\tempdescriptions.txt"))
                File.Delete(appPath + "\\tempdescriptions.txt");

            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            if (File.Exists(appPath + "\\tempfunctions.txt"))
                File.Delete(appPath + "\\tempfunctions.txt");
            if (File.Exists(appPath + "\\tempdescriptions.txt"))
                File.Delete(appPath + "\\tempdescriptions.txt");

            this.Close();
        }

        private void btn_AddFunction_Click(object sender, EventArgs e)
        {
            FunctionEditorAddFunction functionEditorAddFunction = new FunctionEditorAddFunction(functionList);
            functionEditorAddFunction.ShowDialog();
            functionEditorAddFunction.Dispose();

            functionList = new FunctionList();
            functionList.Load(appPath + "\\tempfunctions.txt", appPath + "\\tempdescriptions.txt");

            listbx_Functions.Items.Clear();
            listbx_Functions.Items.AddRange(functionList.Functions);
            listbx_Functions.SelectedIndex = 0;
        }

        private void btn_RemoveFunction_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(@"Are you sure you want to remove function '" + listbx_Functions.SelectedItem.ToString() + @"'?", "Remove Function", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                int index = listbx_Functions.SelectedIndex;
                if (index <= 0) listbx_Functions.SelectedIndex++;
                else            listbx_Functions.SelectedIndex--;
                listbx_Functions.Items.RemoveAt(index);
            }
        }
    }
}
